/**
 * Created by zhourb on 2017/12/17.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
var $alert = require("libs/alert");
var isSale = require('libs/user-type').isSale;
$(function(){
    $('body').on('click','a[name=detail]',function () {
        var rowsData =  $('#goods_change_table').datagrid('getRows');
        var id = rowsData[$(this).attr('row')].goodsId;
        loadGoods(id,"detail");
    });

    $('body').on('click','a[name=entDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'客户公司',true,true);
    });

    $('#tb a[name=search]').on('click',function () {
        $('#goods_change_table').datagrid('load',{
            goodsCode: $('#tb input[name=goodsCode]').val(),
            entName: $('#tb input[name=entName]').val(),
            chName: $('#tb input[name=chName]').val(),
            dateFrom: $('#startDt').data('utc0'),
            dateTo: $('#endDt').data('utc0')
        });
    });
    mainTableInit();
});

//load goods
function loadGoods(id,type) {
    $.ajax({
        url:'api/v1/p/mms/goods/check/load',
        type:'post',
        data:{
            id:id
        },
        dataType:'json',
        success:function (result) {
            if(result.code == 200){
                appendData(result.data,type);
                if(type == "detail"){
                    $('#goodsinfoDetail').modal('show');
                }
            }else{
                $alert(result.message);
            }
        },
        error:function (e) {
            console.error('url: api/v1/p/mms/goods/check/load 接口失败');
        }
    });
}

//append data
function appendData(data,type) {
    $('#id').val(data.id);
    var spans = '';
    $.each(data.pics,function (index,obj) {
        if(index == 0){
            if(type == "audi"){
                $('#goodsinfo div.big-img img').attr('src',obj.filePath);
            }else if(type == "detail"){
                $('#goodsinfoDetail div.big-img img').attr('src',obj.filePath);
            }
        }else{
            spans +="<span><img src='"+obj.filePath+"'></span>";
        }
    })
    if(type == "audi"){
        $('#modal_title1').html(data.chName);
        $('#goodsinfo div.min-img').append(spans);
        var $spans = $('#goodsinfo div.iteam span');
        var table = $('#goodsinfo #impcountries');
        var table2 = $('#goodsinfo #exceptions');
        var picture = $('#check .big-img');
    }else if(type == "detail"){
        $('#modal_title2').html(data.chName);
        $('#goodsinfoDetail div.min-img').append(spans);
        var $spans = $('#goodsinfoDetail div.iteam span');
        var table = $('#goodsinfoDetail #impcountries');
        var table2 = $('#goodsinfoDetail #exceptions');
        var picture = $('#checkDetail .big-img');
    }

    $($spans[0]).html(data.chName);
    $($spans[1]).html(data.enName);
    $($spans[2]).html(data.code);
    $($spans[3]).html(data.spec);
    $($spans[4]).html(data.model);
    $($spans[5]).html(data.brand);

    var batteryType='';
    if(data.batteryType == 'alkaline'){
        batteryType = '碱性电池';
    }else if(data.batteryType == 'lithium'){
        batteryType = '锂电池';
    }else{
        batteryType = '不带电';
    }
    $($spans[6]).html(batteryType);

    var keywords ;
    if(data.keyword.length > 0){
        keywords = data.keyword.join(',');
    }else {
        keywords = '';
    }
    $($spans[7]).html(keywords);

    $($spans[8]).html(data.regGrossWeight);
    $($spans[9]).html(data.regLength);
    $($spans[10]).html(data.regWidth);
    $($spans[11]).html(data.regHeight);
    $($spans[12]).html(data.factGrossWeight);
    $($spans[13]).html(data.factLength);
    $($spans[14]).html(data.factWidth);
    $($spans[15]).html(data.factHigh);

    $($spans[16]).html(data.hsCode);
    $($spans[17]).html(data.manageType);
    $($spans[18]).html(data.exportGoodsTax);

    $($spans[19]).html(data.declaredPrice);

    $($spans[20]).html('');

    $($spans[21]).html(data.deliveryCycle);
    $($spans[22]).html(data.detailWebsite);
    $($spans[22]).parent('a').attr('href',data.detailWebsite);
    $($spans[23]).html(data.summary);
    countryTableInit(table,data.impCountries);
    exceptionsTableInit(table2,data.boxExpInfoVOS);
    newPicsInit(picture,data.newPics);
    if(type == "audi"){
        $("#newareatext").html(data.sendRejectReason);
        $("#areatext").html(data.orderRejectReason)
        if(data.sendAuditSts == 'R'){
            $("#newCheckReult input[name='newCheckReult']").get(1).checked=true;
            $("#oldcheck input[name='checkReult']").get(0).checked=true;
            $('#areatext').hide();
        }else if(data.sendAuditSts == 'U'){
            $("#newCheckReult input[name='newCheckReult']").get(0).checked=true;
            $('#newareatext').hide();
            $('#oldcheck').hide();
        }
        //2017-3-10
        $("#check-sumbit input[name=hsCode]").val(data.hsCode);
        $("#check-sumbit input[name=exportGoodsTax]").val(data.exportGoodsTax);
        $("#check-sumbit input[name=declareName]").val(data.declareName);
        $("#check-sumbit input[name=declareFactor]").val(data.declareFactor);
    }else if(type == "detail"){
        $("#newareatextDetail").html(data.sendRejectReason);
        $("#areatextDetail").html(data.orderRejectReason)

        if(data.sendAuditSts == 'R'){
            $("#newareatextDetail").show();
            $("#newCheckReultDetail input[name='newCheckReult']").get(2).checked=true;
            if(data.orderAuditSts == 'C'){
                $("#oldcheckDetail input[name='checkReult']").get(0).checked=true;
                $('#areatextDetail').hide();
            }else if(data.orderAuditSts == 'P'){
                $("#oldcheckDetail input[name='checkReult']").get(1).checked=true;
                $('#areatextDetail').hide();
            }else if(data.orderAuditSts == 'R'){
                $("#oldcheckDetail input[name='checkReult']").get(2).checked=true;
                $('#areatextDetail').show();
            }
        }else if(data.sendAuditSts == 'U'){
            $("#newCheckReultDetail input[name='newCheckReult']").get(0).checked=true;
            $('#newareatextDetail').hide();
            $('#oldcheckDetail').hide();
            if(data.orderAuditSts == 'C'){
                $("#oldcheckDetail input[name='checkReult']").get(0).checked=true;
                $('#areatextDetail').hide();
            }else if(data.orderAuditSts == 'P'){
                $("#oldcheckDetail input[name='checkReult']").get(1).checked=true;
                $('#areatextDetail').hide();
            }else if(data.orderAuditSts == 'R'){
                $("#oldcheckDetail input[name='checkReult']").get(2).checked=true;
                $('#areatextDetail').show();
            }
        }else if(data.sendAuditSts == 'P'){
            $("#newCheckReultDetail input[name='newCheckReult']").get(1).checked=true
        }
        //2017-3-10
        $("#new_goods_detail input[name=hsCode]").val(data.hsCode);
        $("#new_goods_detail input[name=exportGoodsTax]").val(data.exportGoodsTax);
        $("#new_goods_detail input[name=declareName]").val(data.declareName);
        $("#new_goods_detail input[name=declareFactor]").val(data.declareFactor);
    }
}

function countryTableInit(table,data) {
    var trs = '';
    $.each(data,function (index,obj) {
        trs += "<tr><td>"+obj.country+"</td><td>"+obj.importGoodsTax+"</td><td>"+obj.importGoodsPrice+"</td><td>"+obj.currency+"</td></tr>";
    });
    $(table).find('tbody').html(trs);
}
function exceptionsTableInit(table,data) {
    var trs = '';
    $.each(data,function (index,obj) {
        trs += "<tr><td>"+obj.boxCode+"</td><td>"+obj.code+"</td><td>"+obj.reason
            +"</td><td><a href='javascript:;' name='picture' data-path='"+obj.filePath+"'>查看</a></td></tr>";
    });
    $(table).find('tbody').html(trs);
}
function newPicsInit(picture,data) {
    var imgs = '';
    $.each(data,function (index,obj) {
        imgs += "<img src='"+obj.filePath+"'/>";
    });
    $(picture).html(imgs);
}

function mainTableInit(){
    $("#goods_change_table").datagrid({
        url:'api/v1/p/mms/goods/check/changelist',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        emptyMsg : "暂无数据",
        selectOnCheck : true,
        singleSelect : true,
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                hidden:true
            },{
                field:'goodsCode',
                title:'商品编码',
                width:'10%',
                formatter:function (value,row,index) {
                    return '<a href="javascript:;" class="ellipsis" name="detail" row="'+index+'"><i class="glyphicon"></i>'+value+'</a>';
                }
            },{
                field:'chName',
                title:'商品名称',
                width:'10%'
            },{
                field:'entName',
                title:'客户名称',
                width:'10%',
                formatter:function (value,row,index) {
                    return "<a class='ellipsis' href='javascript:;' ref='company-detail?id="+row.entId+"' name='entDetail' title='"+value+"'>"+value+"</a>";
                }
            },{
                field:'regLength',
                title:'备案尺寸(cm)',
                width:'10%',
                formatter:function (value,row,index) {
                    var l = row.regLength? row.regLength:0;
                    var w = row.regWidth? row.regWidth:0;
                    var h = row.regHeight? row.regHeight:0;
                    return l + 'X' + w + 'X' + h;
                }
            },{
                field:'regGrossWeight',
                title:'备案重量(Kg)',
                width:'8%'
            },{
                field:'factLength',
                title:'实际尺寸(cm)',
                width:'10%',
                formatter:function (value,row,index) {
                    var l = row.factLength? row.factLength:0;
                    var w = row.factWidth? row.factWidth:0;
                    var h = row.factHigh? row.factHigh:0;
                    return l + 'X' + w + 'X' + h;
                }
            },{
                field:'factGrossWeight',
                title:'实际重量(Kg)',
                width:'8%'
            },{
                field:'modifyLength',
                title:'调整后尺寸(cm)',
                width:'10%',
                formatter:function (value,row,index) {
                    var l = row.modifyLength? row.modifyLength:0;
                    var w = row.modifyWidth? row.modifyWidth:0;
                    var h = row.modifyHigh? row.modifyHigh:0;
                    return l + 'X' + w + 'X' + h;
                }
            },{
                field:'modifyGrossWeight',
                title:'调整后重量(Kg)',
                width:'9%'
            },{
                field:'createDt',
                title:'修改时间',
                width:'12%',
                formatter:function (value,row,index) {
                    return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                }
            },{
                field:'userName',
                title:'修改账号',
                width:'10%'
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
}